package com.example.aaa;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
    EditText ed_username, ed_userpwd, ed_userpwdr, ed_id, ed_phone;
    Button btn_submit, btn_back;
    SQLiteDatabase db;
    DBOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        helper = new DBOpenHelper(this);
    }

    private void initView() {
        ed_username = findViewById(R.id.UserNameEdit);
        ed_userpwd = findViewById(R.id.PassWordEdit);
        ed_userpwdr = findViewById(R.id.PassWordAgainEdit);
        ed_id = findViewById(R.id.IdEdit);
        ed_phone = findViewById(R.id.PhoneEdit);
        btn_submit = findViewById(R.id.SignUpButton);
        btn_back = findViewById(R.id.BackLoginButton);
        btn_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String userName = ed_username.getText().toString();
        String userPsd = ed_userpwd.getText().toString();
        String confirPsd = ed_userpwdr.getText().toString();
        String userId = ed_id.getText().toString();
        String userPhone = ed_phone.getText().toString();

        switch (view.getId()) {
            //通过判断哪一项为空来提示输入
            case R.id.SignUpButton:
                String name = ed_username.getText().toString();
                String pwd = ed_userpwd.getText().toString();
                String id = ed_id.getText().toString();
                String phone = ed_phone.getText().toString();
                if (name.isEmpty()) {
                    Toast.makeText(RegistActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pwd.isEmpty()) {
                    Toast.makeText(RegistActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (id.isEmpty()) {
                    Toast.makeText(RegistActivity.this, "请输入学号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (phone.isEmpty()) {
                    Toast.makeText(RegistActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                //判断两次密码是否一致
                if (!userPsd.equals(confirPsd)) {
                    Toast.makeText(RegistActivity.this, "两次输入的密码不一致，注册失败", Toast.LENGTH_SHORT).show();
                } else {
                    //密码一致注册成功并且存入数据库
                    SharedPreferences sp = getSharedPreferences("regInfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("account", userName);
                    editor.putString("password", userPsd);
                    editor.putString("id", userId);
                    editor.putString("phone", userPhone);
                    editor.commit();
                    //获取创建的数据库对象
                    db = helper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("account", userName);
                    values.put("password", userPsd);
                    values.put("id", userId);
                    values.put("phone", userPhone);
                    db.insert("user", null, values);
                    db.close();

                    //获取创建的数据库对象
                    Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    //注册成功后跳转到登陆界面
                    Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();

                }

                break;
            case R.id.BackLoginButton:
                //返回到登陆界面
                Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}